<template>
    <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions(this.chartData)
        },
        initSeries(name, data, color) {
            return {
                name: name,
                smooth: true,
                type: 'line',
                itemStyle: {
                    normal: {
                        color: color,
                        lineStyle: {
                            color: color,
                            width: 2
                        },
                        // areaStyle: {
                        //     color: color
                        // }
                    }
                },
                data: data,
                animationDuration: 2800,
                animationEasing: 'quadraticOut'
            }
        },
        setOptions(chartMaps) {
            //chartData [{name1:{xx:'',xx:''},name2:{xx:'',xx:''}}]
            //多定义几种颜色 后期用
            let colors = ['#3888fa','#FF005A']
            //1-取消一条线
            let lineNames = Object.keys(chartMaps)
            let lineMap = chartMaps[lineNames[0]]
            //从这条线取出所有的xData TODO 传入的key 可能不同 需要重新排序
            let xData = Object.keys(lineMap)
            let series = []
            //遍历所有的线 组成数据
            for (let i = 0; i < lineNames.length; i++) {
                let temp = this.initSeries(lineNames[i], Object.values(chartMaps[lineNames[i]]), colors[i])
                series.push(temp)
            }
            this.chart.setOption({
                xAxis: {
                    data: xData,
                    boundaryGap: false,
                    axisTick: {
                        show: false
                    }
                },
                grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    axisTick: {
                        show: false
                    }
                },
                legend: {
                    data: lineNames
                },
                series: series
            })
        }
    }
}
</script>
